c语言 整数和浮点数_C ++处理整数和浮点数

c语言 整数和浮点数

C ++中的所有数字 ( All About Numbers in C++ )

In C++ there are two types of numbers. Ints and floats. There are also variants of these types that hold bigger numbers, or only unsigned numbers but they are still ints or floats.

在C ++中,有两种类型的数字。 整数浮点数 。 这些类型也有一些变体,可以容纳更大的数字,或仅保留无符号数字,但它们仍然是整数或浮点数。

An int is a whole number like 47 without a decimal point. You can't have 4.5 babies or loop 32.9 times. You can have $25.76 if you use a float. So when you create your program, you must decide which type to use.

一个整数是一个像47这样的整数,没有小数点。 您不能生育4.5个婴儿,也不能循环32.9次。 如果使用浮点数,则可以有$ 25.76。 因此,在创建程序时,必须确定要使用的类型。

为什么不只使用浮标呢? ( Why not Just Use Floats? )

This is what some scripting languages do? Because it's inefficient, floats take up more memory and are generally slower than ints. Also, you cannot easily compare two floats to see if they are equal like you can with ints.

这是某些脚本语言做什么? 由于浮动效率低下,因此浮动占用更多内存,并且通常比整数慢。 而且,您无法像使用int一样轻松地比较两个浮点数以查看它们是否相等。

To manipulate numbers you have to store them in memory. Because the value can be easily changed, it's called a variable.

要操纵数字,您必须将其存储在内存中。 由于该值可以轻松更改,因此称为变量。

The compiler that reads your program and converts it into machine code needs to know what type it is, i.e. whether it's an int or a float, so before your program uses a variable, you must declare it.

读取程序并将其转换为机器代码的编译器需要知道它是什么类型,即它是int还是float,因此在程序使用变量之前,必须对其进行声明

Here's an example.

这是一个例子。

int Counter =0;
float BasicSalary;

You'll notice that the Counter variable is set to 0. This is an optional initialization. It's a very good practice to initialize variables. If you don't initialize and then use them in code without having set an initial value, the variable will start with a random value that may 'break' your code. The value will be whatever was in memory when the program was loaded.

您会注意到Counter变量设置为0。这是可选的初始化。 初始化变量是一个很好的做法。 如果您不进行初始化,然后在未设置初始值的情况下在代码中使用它们,则变量将以可能会“破坏”代码的随机值开头。 该值将是加载程序时内存中的值。

有关Ints的更多信息 ( More about Ints )

What is the biggest number an int can store?. Well, it depends on the type of CPU but it is generally accepted as 32 bits. Because it can hold almost as many negative values as positive, the range of values is +/- 2-32 to 232 or -2,147,483,648 to +2,147,483,647.

一个int可以存储的最大数量是多少? 。 好吧,这取决于CPU的类型,但通常被接受为32位。 因为它可以容纳几乎与正数一样多的负值,所以值的范围为+/- 2 -32至2 32或-2,147,483,648至+2,147,483,647。

This is for a signed int, but there is also an unsigned int that holds zero or positive. It has a range of 0 to 4,294,967,295. Just remember - unsigned ints don't need a sign (like + or -1) in front of them because they are always positive or 0.

这是针对有符号的int,但也有一个无符号的 int,它保持零或正数。 它的范围是0到4,294,967,295。 请记住 -无符号整数不需要在它们前面加一个符号(例如+或-1),因为它们始终为正或0。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值